//企业综合信用echarts highcharts
//-----highstock时间设置
Highcharts.setOptions({
    lang: {
        rangeSelectorZoom: "缩放：",
        //                汉化界面
        months: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
        shortMonths: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
        weekdays: ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
    },
    global: {
        useUTC: false
    }
});
var colors = ['#FE4639', '#FE6F1A', '#CDD313', '#05DB88', '#019fe8'];
var drawEcharts = function (alldata, latest_year, year) {
    var detailinfo, nowYear, latest_score;
    if (year) {
        nowYear = year;
    } else {
        nowYear = latest_year;//获取最新一年或者某一年数据
    }
    detailinfo = alldata[nowYear];
    var minscore = 1000,
        maxscore = 300,//历史纪录纵坐标范围确定
        basicinfo = alldata["basic"],//企业基础信息
        total = alldata["total"],
        nowscore, industryIndex, regionIndex;//当前企业最新一年总分数
    //生成echarts图表所需数据格式
    var years = [], historyscore = [],//历史记录年份和总分
        scoresIndicator = [], scoresName = [], scoresValue = [],//各项指标细分分数
        regionTrendName = [], regionTrendValue = [],//地区分布
        industryTrendName = [], industryTrendValue = [];//行业分布
    var getData = function (type) {
        var data = detailinfo[type];
        scoresIndicator = [], scoresName = [], scoresValue = [];//各项指标细分分数
        for (var k in data) {
            scoresName.push(k);
            scoresValue.push(data[k]);
            if (type == "scores") {
                var obj = {name: k, max: 100};
                scoresIndicator.push(obj);
            }
        }
    };
    var getData2 = function (type, name, value) {
        var data = detailinfo[type];
        for (var k in data) {
            name.push(k);
            value.push(data[k]);
        }
        score > maxscore ? maxscore = score : maxscore;
        score < minscore ? minscore = score : maxscore;
    };
    getData("scores", scoresName, scoresValue);
    getData2("industryTrend", industryTrendName, industryTrendValue);
    getData2("regionTrend", regionTrendName, regionTrendValue);
    //历史记录分数
    for (var k in total) {
        var tal = total[k];
        for (var j in tal) {
            var score = tal[j];
            if (j == nowYear) {
                nowscore = score;
            }
            if (j == latest_year) {
                latest_score = score;
            }
            score > maxscore ? maxscore = score : maxscore;
            score < minscore ? minscore = score : maxscore;
            years.unshift(j);
            historyscore.unshift(score);
        }
    }
    function score_progress() {
        var score_percent = ( parseFloat(latest_score) - 350) / 6;
        $("#c_progress_mask").css("width", score_percent + "%");
        $("#c_progress_value").text(latest_score).css("left", score_percent + "%");
    }

    score_progress();
    //highcharts各个维度展示 雷达图
    var c_dimensions_high = new Highcharts.Chart({
        chart: {
            renderTo: 'c_dimensions_high',
            polar: true,
            type: 'area',
            events: {
                click: function (event) {
                    var ele = event.srcElement.className.baseVal;
                    var bg = ele.indexOf("highcharts-background");
                    if (bg == -1) {
                        var name = event.srcElement.innerHTML;
                        barClick(name);
                    }
                }
            }
        },
        title: {
            text: '',
            x: -800
        },
        pane: {
            size: '80%'
        },
        xAxis: {
            categories: scoresName,
            tickmarkPlacement: 'on',
            lineWidth: 0
        },
        yAxis: {
            show: false,
            gridLineInterpolation: 'polygon',
            lineWidth: 0,
            min: 0,
            max: 100,
            labels: {
                enabled: false
            }
        },
        plotOptions: {
            area: {
                dataLabels: {
                    enabled: true
                }
            }
        },
        tooltip: {
            shared: true,
            pointFormat: '<span style="color:{point.color}">\u25CF</span><b> {point.y}</b>'
        },
        legend: {
            enabled: false
        },
        series: [{
            name: nowYear + "年" + '各个维度评分',
            data: scoresValue,
            pointPlacement: 'on'
        }]
    });
    ////各项维度的细分 柱状图
    var c_dimensions_bar = new Highcharts.Chart({
        chart: {
            renderTo: 'c_dimensions_bar',
            type: 'bar'
        },
        title: {
            text: ""
        },
        xAxis: {
            categories: ['管理团队', '资本实力', '发展能力', '经营稳定', '经营绩效']
        },
        tooltip: {
            enabled: false
        },
        yAxis: {
            min: 0,
            title: {
                text: ''
            },
            labels: {
                enabled: true,
                aligh: "right"
            }
        },
        plotOptions: {
            bar: {
                dataLabels: {
                    enabled: true
                }
            }
            //series: {
            //    colorByPoint: true
            //}
        },
        legend: {
            enabled: false
        },
        credits: {
            enabled: false
        },
        series: [{
            name: nowYear + '年各个维度评分',
            data: scoresValue
        }]
    });
    // var c_industry_bar = new Highcharts.Chart({
    //     chart: {
    //         renderTo: 'c_industry_bar',
    //         type: 'area',
    //         events: {
    //             click: function (event) {
    //                 var ele = event.srcElement.className.baseVal;
    //                 var bg = ele.indexOf("highcharts-area");
    //                 if (bg != -1) {
    //                     gotoDetail("industry", "", 0);
    //                 }
    //             }
    //         }
    //     },
    //     title: {
    //         text: ""
    //     },
    //     xAxis: {
    //         categories: industryTrendName
    //     },
    //     yAxis: {
    //         minorGridLineColor: '#E0E0E0',
    //         tickInterval: '200',
    //         title: {
    //             text: ''
    //         },
    //         plotLines: [{
    //             color: colors[0],
    //             dashStyle: 'solid',
    //             value: nowscore,
    //             width: 2,
    //             label: {
    //                 useHTML: true,
    //                 align: "right",
    //                 text: '<div class="bubble_box"><p class="bubble_text">当前企业</p><span class="bubble">' + nowscore + '</span></div>',
    //                 y: -20
    //             },
    //             events: {
    //                 mouseover: function () {
    //                 }
    //             }
    //         }]
    //     },
    //     plotOptions: {
    //         series: {
    //             cursor: 'pointer',
    //             events: {
    //                 click: function (e) {
    //                     var name = e.point.category;
    //                     var id = jQuery.parseJSON(indusJson)[name];
    //                     if (id) {
    //                         gotoDetail("industry", id, 1);
    //                     } else {
    //                         gotoDetail("industry", id, 0);
    //                     }
    //                 }
    //             }
    //         }
    //     },
    //     tooltip: {
    //         pointFormat: '<span style="color:{point.color}">\u25CF</span><b> {point.y}</b>'
    //     },
    //     series: [{
    //         name: nowYear + "年" + '行业信用评分走势',
    //         data: industryTrendValue
    //     }]
    // });
    // var c_region_bar = new Highcharts.Chart({
    //     chart: {
    //         renderTo: 'c_region_bar',
    //         type: 'area',
    //         events: {
    //             click: function (event) {
    //                 var ele = event.srcElement.className.baseVal;
    //                 var bg = ele.indexOf("highcharts-area");
    //                 if (bg != -1) {
    //                     gotoDetail("region", "", 0);
    //                 }
    //             }
    //         }
    //     },
    //     title: {
    //         text: ""
    //     },
    //     xAxis: {
    //         categories: regionTrendName
    //     },
    //     yAxis: {
    //         minorGridLineColor: '#E0E0E0',
    //         tickInterval: '200',
    //         title: {
    //             text: ''
    //         },
    //         plotLines: [{
    //             color: colors[0],
    //             dashStyle: 'solid',
    //             value: nowscore,
    //             width: 2,
    //             label: {
    //                 useHTML: true,
    //                 align: "right",
    //                 text: '<div class="bubble_box"><p class="bubble_text">当前企业</p><span class="bubble">' + nowscore + '</span></div>',
    //                 y: -20
    //             },
    //             events: {
    //                 mouseover: function () {
    //                 }
    //             }
    //         }]
    //     },
    //     plotOptions: {
    //         series: {
    //             cursor: 'pointer',
    //             events: {
    //                 click: function (e) {
    //                     var name = e.point.category;
    //                     var id = jQuery.parseJSON(regionJson)[name];
    //                     if (id) {
    //                         gotoDetail("region", id, 1);
    //                     } else {
    //                         gotoDetail("region", id, 0);
    //                     }
    //                 }
    //             }
    //         }
    //     },
    //     tooltip: {
    //         pointFormat: '<span style="color:{point.color}">\u25CF</span><b> {point.y}</b>'
    //     },
    //     series: [{
    //         name: nowYear + "年" + '企业信用地区分布',
    //         data: regionTrendValue
    //     }]
    // });
    var createDScoreTable = function () {
       var thstext = ["年份/维度"];
        //先push总分
        thstext.push("总分");
        var tdstext = [];
        for (var key1 in total) {
            var val = total[key1];
            var tds1 = [];
            for (var key2 in val) {

                //年份 和 每一年总分
                tds1.push(key2);
                //把总分push进去
                tds1.push(val[key2]);
                var oneyear = alldata[key2]["scores"];
                for (var key3 in oneyear) {
//每一年各个维度分数
                    if (thstext.indexOf(key3) == -1) {
                        thstext.push(key3);
                    }
                    tds1.push(oneyear[key3]);
                }

                tdstext.push(tds1);
            }
        }
        var trs = $("<tr></tr>"),
            tabth, tabtr;
        for (var i = 0, len = thstext.length; i < len; i++) {
            var td1 = $("<th class='table_th'></th>").append(thstext[i]);
            tabth = trs.append(td1);
        }
        tables.append(tabth);
        for (var l = 0, lenl = tdstext.length; l < lenl; l++) {
            trs = $("<tr></tr>");
            var onetd = tdstext[l];
            for (var m = 0, lenm = onetd.length; m < lenm; m++) {
                var td2 = $("<td></td>").append(onetd[m]);
                tabtr = trs.append(td2);
            }
            trs = $("<tr></tr>");
            tables.append(tabtr);
        }
    };
    var tables = $("#detailscores");
    if ($("#detailscores tr").length == 0) {
        createDScoreTable();
    }
};